package ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fy.fyzf.R;
import com.fy.fyzf.bean.DaiKanListBean;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.utils.AppUtils;

import java.util.List;

import ui.activity.HouseDetailActivity;
import ui.activity.WithGlassesHousingActivity;

public class DaiKanAdapter1 extends SectionedBaseAdapter{
    private List<DaiKanListBean> listBeans;
    private Context mContext;
    public DaiKanAdapter1(Context context) {
        mContext = context;
    }

    public void setData(List<DaiKanListBean> list){
        listBeans = list;
        notifyDataSetChanged();
    }
    @Override
    public Object getItem(int section, int position) {
        return null;
    }

    @Override
    public long getItemId(int section, int position) {
        return 0;
    }

    @Override
    public int getSectionCount() {
        return listBeans.size();
    }

    @Override
    public int getCountForSection(int section) {
        return listBeans.get(section).getHousingFollows().size();
    }

    @Override
    public View getItemView(int section, int position, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
       /* if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.item_with_glass_note, null);
        } else {
            layout = (LinearLayout) convertView;
        }*/
        LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = (LinearLayout) inflator.inflate(R.layout.item_with_glass_note, null);
        //添加
        EditText editText = layout.findViewById(R.id.et_note);
        layout.findViewById(R.id.tv_add_note).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listBeans.get(section).getHousingFollows().add(0,new DaiKanListBean.HousingFollowsBean(" ", " ", ""));
                notifyDataSetChanged();
            }
        });

        //保存
        layout.findViewById(R.id.tv_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WithGlassesHousingActivity mContext = (WithGlassesHousingActivity) DaiKanAdapter1.this.mContext;
                String s = editText.getText().toString().trim();
                if (TextUtils.isEmpty(s)) {
                    AppUtils.showToast("请输入内容");
                    return;
                }
                mContext.addHousingNote(listBeans.get(section).getHousingId(),editText.getText().toString().trim());
            }
        });

        editText.setText(listBeans.get(section).getHousingFollows().get(position).getFollowRemark());
        return layout;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        RelativeLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (RelativeLayout) inflator.inflate(R.layout.item_head_with_glass, null);
        } else {
            layout = (RelativeLayout) convertView;
        }

        TextView textView = (TextView) layout.findViewById(R.id.tv_number);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, HouseDetailActivity.class);
                intent.putExtra("housingid", listBeans.get(section).getHousingId());
                mContext.startActivity(intent);
            }
        });
        textView.setText(listBeans.get(section).getHousingNo()+"");
        return layout;
    }
}
